package com.iss.ljx.login.controller;

import com.iss.ljx.login.entity.MenuLevelEntity;
import com.iss.ljx.login.service.LoginService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 流星
 */
@Controller
public class MainController {
    private Logger logger = LogManager.getLogger(this.getClass().getName());
    @Resource
    private LoginService loginService;
    @RequestMapping("/")
    public String main(HttpServletRequest request){
        List<MenuLevelEntity> list = loginService.queryMenuLevelList();

        HttpSession session = request.getSession();
        session.setAttribute("menu",list);
        return "sm/main";
    }
    @RequestMapping("/welcome")
    public String index(){
        return "sm/welcome";
    }


    @RequestMapping("/getServiceMenu")
    @ResponseBody
    public List<MenuLevelEntity> getServiceMenu(HttpServletRequest request){
        String menuId = request.getParameter("menuId");
        if(menuId == null){
            return null;
        }
        HttpSession session = request.getSession();
        List<MenuLevelEntity> list = (List<MenuLevelEntity>) session.getAttribute("menu");
        for(MenuLevelEntity levelEntity: list){
            if(menuId.equals(levelEntity.getMenuId())){
//                logger.info("****{}",levelEntity.getChildrenList());
                return levelEntity.getChildrenList();
            }
        }
        return null;
    }
}
